﻿using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.VersionControl.Client;
using NLog;

namespace TfsGiggle.Core
{
    /// <summary>
    /// Workitemtracking services
    /// </summary>
    public static class TfsWorkItem
    {
        private static LoggerWithEventId _logger = (LoggerWithEventId)LogManager.GetCurrentClassLogger(typeof(LoggerWithEventId));

        /// <summary>
        /// Isolated due to VersionControl.Client
        /// </summary>
        /// <param name="item"></param>
        /// <param name="workItems"></param>
        public static void AddWorkItems(IChangeset item, Dictionary<int, string> workItems)
        {
            if (item == null) throw new ArgumentNullException("item");
            if (workItems == null) throw new ArgumentNullException("workItems");

            _logger.Trace("Changeset:{0}", item.ChangesetId);
            if (item.WorkItems == null || item.WorkItems.Length == 0) return;
            try
            {
                foreach (var workItem in item.WorkItems)
                {
                    if (!workItems.ContainsKey(workItem.Id))
                    {
                        workItems.Add(workItem.Id, workItem.Title);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorExceptionWithEventId(EventId.WorkitemsAdd, "Exception caught", ex);
            }
            _logger.Trace("exit");
        }
    }
}